<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    var socket;
    if (window.WebSocket) {
        socket = new WebSocket("ws://localhost:9099/myapp");
        socket.onmessage = function (event) {
            alert("data from websocket: " + event.data);
        };
        socket.onopen = function (event) {
            alert("Web Socket 打开!");
        };
        socket.onclose = function (event) {
            console.log("Web Socket 关闭.");
        };
    } else {
        alert("Your browser does not support Websockets. (Use Chrome)");
    }
 
    function send(message) {
        if (!window.WebSocket) {
            return;
        }
        if (socket.readyState == WebSocket.OPEN) {
            socket.send(message);
        } else {
            alert("The socket is not open.");
        }
    }
</script>
<form onsubmit="return false;">
    <input type="text" name="message" value="Hello, World!"/>
    <input type="button" value="Send Web Socket Data" onclick="send(this.form.message.value)"/>
</form>
</body>
</html>
复制代码